package com.yanqu.road.server.cmd.wingroom.message;

import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomMessageModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

/**
 * @author ：zfr
 * @date ：2022/3/18 9:26
 */
@Cmd(code = GameProtocol.S_WING_ROOM_NEW_MESSAGE, desc = "留言板留言")
public class NewMessageCmd extends AbstractCommand {
    private long ownerServerId;
    private long ownerUserId;
    private String content;
    private int noteStyle;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.NewMessageReqMsg reqMsg = WingRoomProto.NewMessageReqMsg.parseFrom(packet.getBytes());
        ownerServerId = reqMsg.getOwnerServerId();
        ownerUserId = reqMsg.getOwnerUserId();
        content = reqMsg.getContent();
        noteStyle = reqMsg.getNoteStyle();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_WING_ROOM_NEW_MESSAGE);
        if(null != gameEditLimit){
            long nowTime = System.currentTimeMillis();
            if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }
        }
        /**
         * 如果被禁用 则返回系统未开放
         */
        if (ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_WING_ROOM_NEW_MESSAGE)) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        player.getModule(WingRoomMessageModule.class).newMessage(ownerServerId, ownerUserId, content, noteStyle);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret){
            WingRoomProto.NewMessageRespMsg.Builder respMsg = WingRoomProto.NewMessageRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_WING_ROOM_NEW_MESSAGE, respMsg);
        }
    }
}
